/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

var currentPage = 0;
var elementPerPage = 1;
var myList = new Array();
            
// Constructeur de l'objet importé
function Adherent(nom, prenom, sexe, dateNaiss, numLicence)
{
    this.nom = nom;
    this.prenom = prenom;
    this.sexe = sexe;
    this.dateNaiss = dateNaiss;
    this.numLicence = numLicence;
}
            
function loadData()
{
        <c:forEach var="element" items="${myList}">
        myList.push(new Adherent("${element.nom}", "${element.prenom}", "${element.sexe}", "${element.dateNaissance}", "${element.numLicence}"));
        </c:forEach>
    }
            
$(document).ready(function() {
    loadData();
    nextPage();
});
            
function buildTableHeader()
{
    $("#mapTable").append("\n\
                <tr>\n\
                    <td>\n\
                        NOM\n\
                    </td>\n\
                    <td>\n\
                        Prénom\n\
                    </td>\n\
                    <td>\n\
                        Sexe\n\
                    </td>\n\
                    <td>\n\
                        Date de naissance\n\
                    </td>\n\
                    <td>\n\
                        N° Licence\n\
                    </td>\n\
                </tr>");
}
            
function nextPage()
{
    if(currentPage + 1 <= <%=myList.size()%>)
    {
        $("#list-table").empty();
        buildTableHeader();
        var start = currentPage * elementPerPage;
        var end = (currentPage + 1) * elementPerPage;
                
        buildTable(start, end);
        currentPage++;
        buildTableFooter();
    }
}
function previousPage()
{
    if(currentPage - 1 > 0)
    {
        $("#list-table").empty();
        buildTableHeader();
        var start = (currentPage - 2) * elementPerPage;
        var end = (currentPage - 1) * elementPerPage;

        buildTable(start, end);
        currentPage--;
        buildTableFooter();
    }
}
function buildTable(begin, end)
{
    for(i = begin; i < end; i++)
    {
        $("#list-table").append("\n\
                        <tr>\n\
                            <td>" + myList[i].nom + "</td>\n\
                            <td>" + myList[i].prenom + "</td>\n\
                            <td>" + myList[i].sexe + "</td>\n\
                            <td>" + myList[i].dateNaiss + "</td>\n\
                            <td>" + myList[i].numLicence + "</td>\n\
                        </tr>");
    }
}
            
function buildTableFooter()
{
    $("#list-table").append('\n\
                <tr>\n\
                    <td colspan="4">\n\
                        <button id="backBtn" onclick="previousPage()"><</button>\n\
                        Affichage des cartes <input id="currentPage" type="text" readonly="true" value="' + currentPage + '"/> à \n\
                        <input id="currentPage" type="text" readonly="true" value="' + currentPage*elementPerPage + '"/> sur un total de \n\
                        <input id="totalPage" type="text" readonly="true" value="<%=myList.size()%>"/>\n\
                        <button id="nextBtn" onclick="nextPage()">></button>\n\
                    </td>\n\
                </tr>');
}
